package com.bawei.library_common.log

import android.graphics.BitmapFactory
import com.bawei.library_common.log.logger.EmailLogger
import com.bawei.library_common.log.logger.FileLogger
import com.bawei.library_common.log.logger.ILogger
import com.bawei.library_common.log.logger.LogCatLogger

/**
 * @Author : yaotianxue
 * @Time : On 2023/5/23 10:13
 * @Description : Logger 构建者+策略模式
 */
//主构造
class Logger(private val TAG:String, private val level:LoggerLevel, private val loggerStrategy: LoggerStrategy, private val isDebug:Boolean, private val saveUrl:String) {
    //次构造：必须继承主构造
    constructor(builder: Builder):this(
        builder.TAG,
        builder.level,
        builder.loggerStrategy,
        builder.isDebug,
        builder.saveUrl
    )

    //策略模式创建对应的logger
    private var logger:ILogger = when(loggerStrategy){
        LoggerStrategy.EMAIL -> EmailLogger()
        LoggerStrategy.FILE -> FileLogger()
        LoggerStrategy.NET -> throw IllegalArgumentException("该方案未实现")
        LoggerStrategy.LOGCAT -> LogCatLogger()
    }
    //初始化代码块：为logger配置参数
    init {
        logger.setLoggerLevel(level)
        logger.setLogTAG(TAG)
        logger.setDebug(isDebug)
        logger.setSaveUrl(saveUrl)

    }




    /**
     * 输出Debug log
     */
    fun d(Tag:String,log:String){
        logger.d(Tag,log)
    }

    /**
     * 输出Warn log
     */
    fun w(Tag:String,log:String){
        logger.w(Tag,log)
    }

    /**
     * 输出Verbose log
     */
    fun v(Tag:String,log:String){
        logger.v(Tag,log)
    }

    /**
     * 输出Error log
     */
    fun e(Tag:String,log:String){
        logger.e(Tag,log)
    }
    /**
     * 输出Info log
     */
    fun i(Tag:String,log:String){
        logger.i(Tag,log)
    }




    class Builder{
        var TAG:String = "默认TAG"
        var level:LoggerLevel = LoggerLevel.Debug //等级
        var loggerStrategy: LoggerStrategy = LoggerStrategy.LOGCAT //默认策略
        var isDebug:Boolean = true //是否Debug
        var saveUrl:String = "默认路径" //路径

        fun setTAG(TAG:String):Builder{
            this.TAG = TAG
            return this
        }
        fun setLevel(level:LoggerLevel):Builder{
            this.level = level
            return this
        }

        fun setLoggerStrategy(loggerStrategy: LoggerStrategy):Builder{//设置策略
            this.loggerStrategy = loggerStrategy
            return this
        }

        fun isDebug(isDebug: Boolean):Builder{
            this.isDebug = isDebug
            return this
        }


        fun setSaveUrl(saveUrl: String):Builder{
            this.saveUrl = saveUrl
            return this
        }

        fun  build():Logger{
            return Logger(this)
        }



    }
}